package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CateGoryService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author : [wangtao]
 * @className : SetmealController
 * @description : []
 * @createTime : [2022/10/12 20:24]
 * @updateTime : [2022/10/12 20:24]
 */
@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {
   @Autowired
   private SetmealService setmealService;
   @Autowired
   private CateGoryService cateGoryService;
    @PostMapping
    public R<String> add(@RequestBody SetmealDto setmealDto){
      setmealService.add(setmealDto)  ;
      return R.success("添加成功");
    }

    @GetMapping("/page")
    public R<Page<Setmeal>> pageShow(int page,int pageSize,String name){
        Page<Setmeal> pageInfo=new Page<>();
        LambdaQueryWrapper<Setmeal> lqw=new LambdaQueryWrapper<>();
        lqw.eq(StringUtils.isNotEmpty(name),Setmeal::getName,name).orderByAsc(Setmeal::getCreateTime);
       setmealService.page(pageInfo,lqw);
        List<Setmeal> records = pageInfo.getRecords();
        List<Long> collect = records.stream().map(id -> id.getCategoryId()).collect(Collectors.toList());
        List<Category> categories = cateGoryService.listByIds(collect);
        Map<Long, String> map = categories.stream().collect(Collectors.toMap(Category::getId, Category::getName));
        for (Setmeal record : records) {
            Long categoryId = record.getCategoryId();
            String categoryname = map.get(categoryId);
            record.setCategoryName(categoryname);
        }
        return R.success(pageInfo);
    }

    @DeleteMapping
    public R delete(@RequestParam List<Long> ids) {
        R<String> delete = setmealService.delete(ids);
        return R.success(delete);
    }
@PostMapping("/status/{id}")
    public R updateStatus(@PathVariable Long id,@RequestParam List<Long> ids){
      LambdaUpdateWrapper<Setmeal> lqw=new LambdaUpdateWrapper<>();
      lqw.in(Setmeal::getId,ids).set(Setmeal::getStatus,id);
//      LambdaQueryWrapper<Setmeal> lqw1=new LambdaQueryWrapper<>();
//      lqw1.eq(Setmeal::getId,ids);
//    List<Setmeal> list = setmealService.list(lqw1);
//    for (Setmeal setmeal : list) {
//        setmeal.setStatus(Math.toIntExact(id));
//    }
    setmealService.update(lqw);

    return R.success("更改成功");
}

    @GetMapping("/list")
    public R list(Setmeal setmeal) {
        log.info("setmeal:{}", setmeal);
        Long categoryId = setmeal.getCategoryId();
        Integer status = setmeal.getStatus();
        LambdaQueryWrapper<Setmeal> lqw = new LambdaQueryWrapper<>();
        lqw.eq(status != null, Setmeal::getStatus, status).eq(categoryId != null, Setmeal::getCategoryId, categoryId);
        List<Setmeal> list = setmealService.list(lqw);
        return R.success(list);
    }



}
